//
//  HJonesNetting.m
//  HiCar
//
//  Created by 丁一 on 2019/1/28.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "HJonesNetting.h"
#import "GXDESTools.h"
#import "tool.h"

@implementation HJonesNetting

+ (void)JonesNetting_POST:(NSDictionary*)dict andURL:(NSString *)Url andtimeout:(double)TimeOut finished:(void(^)(NSError* error,NSDictionary* resultDict))block passmanger:(void(^)(NSURLSessionDataTask *manger))blockmanger andprogress:(void(^)(NSProgress *CProgress))progressBlcok{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval=TimeOut;

    NSString *Get_url=[NSString stringWithFormat:@"%@%@",API_URL,Url];
    
    NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc] initWithDictionary:dict];
    
    if ([Url isEqualToString:App_UploadVerify]) {
        NSMutableDictionary *tmpDictp = [[NSMutableDictionary alloc] initWithDictionary:dict];
        [tmpDictp removeObjectForKey:@"card"];
        [tmpDictp removeObjectForKey:@"driving"];
        [tmpDictp removeObjectForKey:@"travel"];
        NSString *pam = [tool parameterMakerOrderTostring:tmpDictp];
        
        pam = [pam stringByReplacingOccurrencesOfString:@" "withString:@""];
        
        NSString *qinss = [[tool encodeToPercentEscapeString:pam] lowercaseString];
        
        [tmpDic setValue:[GXDESTools encryptWithText:qinss] forKey:@"sign"];
    }else{
        NSString *pam = [tool parameterMakerOrderTostring:dict];
        
        pam = [pam stringByReplacingOccurrencesOfString:@" "withString:@""];
        
        NSString *qinss = [[tool encodeToPercentEscapeString:pam] lowercaseString];
        
        [tmpDic setValue:[GXDESTools encryptWithText:qinss] forKey:@"sign"];
    }
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURLSessionDataTask *task = [manager POST:Get_url
                                    parameters:tmpDic
                                      progress:^(NSProgress * _Nonnull uploadProgress) {
                                          if (progressBlcok) {
                                              progressBlcok(uploadProgress);
                                          }
                                      }
                                       success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                           [SVProgressHUD dismiss];
                                           if (block) {
                                               NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                                               if ([[data objectForKey:@"Code"] longLongValue]==1000) {
                                                   
                                               } else if ([[data objectForKey:@"Code"] longLongValue]==2000) {
                                                   [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                               } else if ([[data objectForKey:@"Code"] longLongValue]==3000) {
                                                   [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                               } else if ([[data objectForKey:@"Code"] longLongValue]==4000) {
                                                   [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                               }
                                               block(nil,data);
                                           }
                                           [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                                       }
                                       failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                           [SVProgressHUD dismiss];
                                           if (block) {
                                               block((NSError *)error,nil);
                                           }
                                           [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                                       }
                                  ];
    if (blockmanger) {
        blockmanger(task);
    }
}


+ (void)JonesNetting_GET:(NSDictionary*)dict andURL:(NSString *)Url andtimeout:(double)TimeOut finished:(void(^)(NSError* error,NSDictionary* resultDict))block passmanger:(void(^)(NSURLSessionDataTask *manger))blockmanger andprogress:(void(^)(NSProgress *CProgress))progressBlcok{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    
    manager.requestSerializer.timeoutInterval=TimeOut;
    
    NSString *Get_url=[NSString stringWithFormat:@"%@%@",API_URL,Url];

    NSURLSessionDataTask *task = [manager GET:Get_url
                                   parameters:dict
                                     progress:^(NSProgress * _Nonnull uploadProgress) {
                                         if (progressBlcok) {
                                             progressBlcok(uploadProgress);
                                         }
                                     }
                                      success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                          [SVProgressHUD dismiss];
                                          if (block) {
                                              NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                                              if ([[data objectForKey:@"Code"] longLongValue]==1000) {
                                                  
                                              } else if ([[data objectForKey:@"Code"] longLongValue]==2000) {
                                                  [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                              } else if ([[data objectForKey:@"Code"] longLongValue]==3000) {
                                                  [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                              } else if ([[data objectForKey:@"Code"] longLongValue]==4000) {
                                                  [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                              }
                                              block(nil,data);
                                          }
                                          [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                                      }
                                      failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                          [SVProgressHUD dismiss];
                                          if (block) {
                                              block((NSError *)error,nil);
                                          }
                                          [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                                      }
                                  ];
    
    if (blockmanger) {
        blockmanger(task);
    }
}

+ (void)JonesNettingText_GET:(NSDictionary*)dict andURL:(NSString *)Url andtimeout:(double)TimeOut finished:(void(^)(NSError* error,NSDictionary* resultDict))block passmanger:(void(^)(NSURLSessionDataTask *manger))blockmanger andprogress:(void(^)(NSProgress *CProgress))progressBlcok{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",nil];
    
    manager.requestSerializer.timeoutInterval=TimeOut;
    
    NSString *Get_url=[NSString stringWithFormat:@"%@%@",API_URL,Url];
    
    NSURLSessionDataTask *task = [manager GET:Get_url
                                   parameters:dict
                                     progress:^(NSProgress * _Nonnull uploadProgress) {
                                         if (progressBlcok) {
                                             progressBlcok(uploadProgress);
                                         }
                                     }
                                      success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                          [SVProgressHUD dismiss];
                                          if (block) {
                                              NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                                              if ([[data objectForKey:@"Code"] longLongValue]==1000) {
                                                  
                                              } else if ([[data objectForKey:@"Code"] longLongValue]==2000) {
                                                  [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                              } else if ([[data objectForKey:@"Code"] longLongValue]==3000) {
                                                  [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                              } else if ([[data objectForKey:@"Code"] longLongValue]==4000) {
                                                  [SVProgressHUD showErrorWithStatus:[data objectForKey:@"Message"]];
                                              }
                                              block(nil,data);
                                          }
                                          [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                                      }
                                      failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                          [SVProgressHUD dismiss];
                                          if (block) {
                                              block((NSError *)error,nil);
                                          }
                                          [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                                      }
                                  ];
    
    if (blockmanger) {
        blockmanger(task);
    }
}

@end
